package HttpUtils;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.conn.routing.HttpRoute;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.*;

/**
 * 主文件
 */
public class HttpClientUti {
    private static PoolingHttpClientConnectionManager poolingHttpClientConnectionManager = null;
    private static HttpClientBuilder httpClientBuilder = null;
    private static RequestConfig config =null;
    private static int MAXCONNECTION = 10;
    private static int DEFAULTMAXCONNECTION = 5;
    private static String IP = "cnivi.com.cn";
    private static int PORT = 80;

    static {
        //静态代码块
        // 设置http请求参数
        config = RequestConfig.custom()
                .setConnectTimeout(5000)
                .setSocketTimeout(5000)
                .setConnectionRequestTimeout(5000)
                .build();

        HttpHost host = new HttpHost(IP,PORT);
        poolingHttpClientConnectionManager = new PoolingHttpClientConnectionManager();
        poolingHttpClientConnectionManager.setMaxTotal(MAXCONNECTION);//客户端总并行最大连接数
        poolingHttpClientConnectionManager.setDefaultMaxPerRoute(DEFAULTMAXCONNECTION);//每个主机的最大并行连接数
        poolingHttpClientConnectionManager.setMaxPerRoute(new HttpRoute(host),20);
        httpClientBuilder = HttpClients.custom();
        httpClientBuilder.setConnectionManager(poolingHttpClientConnectionManager);
    }

    public static CloseableHttpClient getConnection(){
        CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
        return closeableHttpClient;
    }

    public static HttpUriRequest getRequestMethod(Map<String,String> map, String url,String method){
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        Set<Map.Entry<String,String>> set = map.entrySet();
        for(Map.Entry<String,String> entry : set){
            String name = entry.getKey();
            String value = entry.getValue();
            NameValuePair nameValuePair = new BasicNameValuePair(name,value);
            params.add(nameValuePair);
        }
        HttpUriRequest reqMethod = null;
        if ("post".equals(method)) {
            reqMethod = RequestBuilder.post().setUri(url)
                    .addParameters(params.toArray(new BasicNameValuePair[params.size()]))
                    .setConfig(config).build();
        }else if("get".equals(method)){
            reqMethod = RequestBuilder.get().setUri(url)
                    .addParameters(params.toArray(new BasicNameValuePair[params.size()]))
                    .setConfig(config).build();
        }
        return reqMethod;
    }

    public static void main(String[] args) throws IOException {
        Map<String,String> map = new HashMap<String, String>();
        map.put("account","");
        map.put("password","");
        HttpUriRequest request = getRequestMethod(map,"http://cnivi.com.cn/login","post");
        HttpClient client = getConnection();
        HttpResponse response = client.execute(request);

        if (response.getStatusLine().getStatusCode() == 200) {
            HttpEntity entity = response.getEntity();
            String message = EntityUtils.toString(entity,"utf-8");
            System.out.println(message);
        }else {
            System.out.println("..失败");
        }
    }
}
